<template>
	<view class="">
		<view v-for="(item,index) in fileList" :key="index" class="view">
			<view style="word-break:break-all; display: flex;align-items: flex-end; justify-content: space-between;">
				<view>
					{{item.fileName}}
				</view>
				<view class="flex">
					<view @click="preview(item)" class="mr-10">
						<uv-icon color="primary" name="eye-fill" size="16"></uv-icon>
					</view>
					<view @click="deleteImage(item)">
						<uv-icon color="primary" name="trash-fill" size="16"></uv-icon>
					</view>
				</view>

			</view>
		</view>
	</view>

</template>

<script>
	import tip from '@/common/util/tip.js'
	import $U from '@/common/lib/util.js'
	export default {
		props: {
			list: {
				type: Array,
				default: () => {
					return []
				}
			},
			limit: {
				type: Number,
				default: 3
			}
		},
		data() {
			return {
				fileList: []
			}
		},
		watch: {
			list: {
				immediate: true,
				handler(newValue) {

					this.fileList = newValue;
				},
				deep: true
			}
		},
		mounted() {
			this.fileList = this.list
		},
		methods: {
			deleteImage(item) {
				uni.showModal({
					content: '是否要删除该文件？',
					success: (res) => {
						if (res.confirm) {
							let index = this.fileList.findIndex(url => url === item)
							if (index !== -1) {
								this.fileList.splice(index, 1)
								this.$emit('update', this.fileList)
							}
						}
					}
				});
			},
			preview(item) {
				let fileSuffix = item.fileSuffix
				let pattern = /(jpeg|jpg|png|gif)$/i;
				let judgment = pattern.test(fileSuffix)
				if (judgment) {

					uni.previewImage({
						current: item.fileSuffix,
						urls: [fileSuffix]
					})
				} else {
					// #ifdef MP-WEIXIN
					uni.downloadFile({
						url: item.fileSuffix,
						success: function(res) {
							let type = $U.getFileExtension(item.fileSuffix)
							var filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								showMenu: true,
								fileType: type,
								success: function(res) {
									console.log('打开文档成功');
								},
								fail: function(err) {
									console.log('打开文档失败', err);
								}
							});
						}
					});
					// #endif

					// #ifdef H5
					if (item.fileSuffix) {

						let link = document.createElement('a');
						link.href = item.fileSuffix;
						link.download = item.fileName; // 设置下载文件名
						document.body.appendChild(link);
						link.click();
						document.body.removeChild(link);
					}
					// if (item.fileSuffix) {
					// 	window.open(item.fileSuffix);
					// }
					// #endif

				}
			},
		},
	}
</script>

<style>
	.view {
		width: 100%;
		padding: 10px 0px;
		border-bottom: 1px solid #dadbde;
	}
</style>